package com.example.graduatetest.view.model;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.ViewModel;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.graduatetest.R;
import com.example.graduatetest.activitys.AskLeaveActivity;
import com.example.graduatetest.activitys.FindCardActivity;
import com.example.graduatetest.activitys.HomeActivity;
import com.example.graduatetest.activitys.NoticeListActivity;
import com.example.graduatetest.activitys.NoticeWriteActivity;
import com.example.graduatetest.activitys.ScoreAllActivity;
import com.example.graduatetest.activitys.ScorePersonActivity;
import com.example.graduatetest.activitys.SendCardActivity;
import com.example.graduatetest.activitys.TeacherPersonActivity;
import com.example.graduatetest.activitys.UploadScoreActivity;
import com.example.graduatetest.adapter.ActionItemAdapter;
import com.example.graduatetest.adapter.StudentItemAdapter;
import com.example.graduatetest.bean.Classes;
import com.example.graduatetest.bean.Icon;
import com.example.graduatetest.bean.Student;
import com.example.graduatetest.bean.User;
import com.example.graduatetest.callback.StudentList;
import com.example.graduatetest.databinding.ActivityAskLeaveBinding;
import com.example.graduatetest.databinding.ActivityClassDetailBinding;
import com.example.graduatetest.model.ClassModel;
import com.example.graduatetest.utils.Constant;

import java.util.ArrayList;
import java.util.List;

public class ClassDetailViewModel extends ViewModel {
    private Activity activity;
    private ActivityClassDetailBinding binding;
    private BaseAdapter mAdapter = null;
    private List<Icon> mData = new ArrayList<>();
    private List<Student> studentList = new ArrayList<>();
    private Handler handler = new Handler();
    private Classes ownClass;
    private User user;

    public void init(Activity activity, ActivityClassDetailBinding binding,Classes ownClass){
        this.activity = activity;
        this.binding = binding;
        this.ownClass = ownClass;
        user = Constant.checkStudentOrTeacher(activity);
        initView();
        toolbarInit();
        initGridView();
        getData();
    }

    //初始课程
    private void toolbarInit(){
        Toolbar toolbar = binding.toolbar4;
        toolbar.setTitle(ownClass.getName());
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.onBackPressed();
            }
        });

        //判断
        SharedPreferences.Editor editor = activity.getApplication().getSharedPreferences("data", Context.MODE_PRIVATE).edit();
        editor.putBoolean("isLeave", false);
        editor.apply();

    }

    //获取数据
    private void getData(){
        ClassModel.getStudentList(ownClass.getId(), new StudentList() {
            @Override
            public void onSuccess(List<Student> list) {
                studentList = list;
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        initRecyclerView();
                    }
                });
                Log.e("haha","获取学生列表成功+"+studentList.size());
            }

            @Override
            public void onFailed() {
                studentList = null;
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        binding.tvNoStudent.setVisibility(View.VISIBLE);
                    }
                });
                Log.e("haha","获取学生列表失败");
            }
        });
    }

    //初始化RecyclerView
    private void initRecyclerView(){
        binding.recyclerView.setVisibility(View.VISIBLE);
        LinearLayoutManager layoutManager = new LinearLayoutManager(activity);
        binding.recyclerView.setLayoutManager(layoutManager);
        if(studentList != null){
            StudentItemAdapter studentItemAdapter = new StudentItemAdapter(studentList);
            binding.recyclerView.setAdapter(studentItemAdapter);
        }
    }
    private void initView(){
        binding.tvClassId.setText("课程号："+ownClass.getId());
        binding.tvClassNumber.setText("同学:"+ownClass.getNumber());
    }

    //初始化网格布局
    private void initGridView(){

        mData.add(new Icon(R.drawable.notice, "通知"));//0
        mData.add(new Icon(R.drawable.data, "作业"));//1
        mData.add(new Icon(R.drawable.card, "打卡"));//2
        mData.add(new Icon(R.drawable.count, "成绩统计"));//3
        mData.add(new Icon(R.drawable.homework, "成绩查询"));//4
        mData.add(new Icon(R.drawable.score, "成绩上传"));//5
        mData.add(new Icon(R.drawable.person_male, "老师简介"));//6
        mData.add(new Icon(R.drawable.class_info, "考勤"));//7
        mData.add(new Icon(R.drawable.ask_class_student, "请假"));//8
        mData.add(new Icon(R.drawable.ask_class_teacher, "批假"));//9




        mAdapter = new ActionItemAdapter(activity.getApplicationContext(), mData);
        binding.gdAction.setAdapter(mAdapter);

        binding.gdAction.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent;
                switch (position){
                    case 0:
                        if(!user.isStudent()){
                            intent = new Intent(activity, NoticeWriteActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.putExtra("classes", ownClass);
                            intent.putExtra("type", "公告");
                            activity.startActivity(intent);
                        }else{
                            intent = new Intent(activity, NoticeListActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.putExtra("classes", ownClass);
                            intent.putExtra("type", "公告");
                            activity.startActivity(intent);
                        }

                        break;
                    case 1:
                        if(!user.isStudent()){
                            intent = new Intent(activity, NoticeWriteActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.putExtra("classes", ownClass);
                            intent.putExtra("type", "作业");
                            activity.startActivity(intent);
                        }else{
                            intent = new Intent(activity, NoticeListActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.putExtra("classes", ownClass);
                            intent.putExtra("type", "作业");
                            activity.startActivity(intent);
                        }
                        break;
                    case 2:
                        if(user.isStudent()){
                            intent = new Intent(activity, FindCardActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.putExtra("class_number", ownClass.getId());
                            activity.startActivity(intent);
                        }else{
                            Toast.makeText(activity, "你没有权限",Toast.LENGTH_SHORT).show();
                        }

                        break;
                    case 3:
                        intent = new Intent(activity, ScoreAllActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("classes", ownClass);
                        activity.startActivity(intent);
                        break;
                    case 4:
                        if (user.isStudent()){
                            intent = new Intent(activity, ScorePersonActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.putExtra("classes", ownClass);
                            activity.startActivity(intent);
                        }else{
                            Toast.makeText(activity, "你没有权限",Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case 5:
                        if(!user.isStudent()){
                            intent = new Intent(activity, UploadScoreActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.putExtra("class_name", ownClass.getName());
                            intent.putExtra("class_number_send", ownClass.getId());
                            activity.startActivity(intent);
                        }else{
                            Toast.makeText(activity, "你没有权限",Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case 6:
                        intent = new Intent(activity, TeacherPersonActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("teacher_id", ownClass.getTeacher_number());
                        activity.startActivity(intent);
                        break;
                    case 7:
                        if(!user.isStudent()){
                            intent = new Intent(activity, SendCardActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            activity.startActivity(intent);
                        }else{
                            Toast.makeText(activity, "你没有权限",Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case 8:
                        if(user.isStudent()){
                            intent = new Intent(activity, AskLeaveActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.putExtra("class_number", ownClass.getId());
                            activity.startActivity(intent);
                        }else{
                            Toast.makeText(activity, "你没有权限",Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case 9:
                        if(!user.isStudent()){
                            SharedPreferences.Editor editor = activity.getApplication().getSharedPreferences("data", Context.MODE_PRIVATE).edit();
                            editor.putBoolean("isLeave", true);
                            editor.apply();
                            intent = new Intent(activity, NoticeListActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.putExtra("classes", ownClass);
                            intent.putExtra("type", "请假");
                            activity.startActivity(intent);
                        }else{
                            Toast.makeText(activity, "你没有权限",Toast.LENGTH_SHORT).show();
                        }
                        break;

                }
                //Toast.makeText(activity, "你点击了~" + position + "~项", Toast.LENGTH_SHORT).show();
            }
        });

    }
}
